package com.product.system.controller;

import cn.hutool.core.lang.tree.Tree;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.product.system.domain.ArgpCropGrowthRecords;
import com.product.system.domain.ArgpCrops;
import com.product.system.domain.ArgpQualityInspectionReport;
import com.product.system.service.ArgpCropsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("ArgpCrops")
public class ArgpCropsController {


    @Autowired
    ArgpCropsService argpCropsService;
    @Autowired
    RedisTemplate redisTemplate;

    @GetMapping  ("Citylist")
    public List<Tree<Object>> Citylist(){

        return    argpCropsService.selectAllAreas();

    }
    @RequestMapping("list")
    public PageInfo<ArgpCrops> list(Integer pageNum){
        PageHelper.startPage(pageNum,5);
       List<ArgpCrops>list= argpCropsService.list();
        PageInfo<ArgpCrops> info = new PageInfo<>(list);
        return info;

    }
    @RequestMapping("add")
    public HashMap<String, Object> add(@RequestBody ArgpCrops argpCrops){
       Boolean save= argpCropsService.add(argpCrops);
        HashMap<String, Object> map = new HashMap<>();
        if (save){
            map.put("code",200);
            map.put("msg","添加成功");
        }else{
            map.put("code",500);
            map.put("msg","添加失败");
        }


        return map;

    }
    @RequestMapping("Recordadd")
    public HashMap<String, Object> Recordadd(@RequestBody ArgpQualityInspectionReport  argpQualityInspectionReport){

        Long increment = redisTemplate.opsForValue().increment("农作物id:" + argpQualityInspectionReport.getCropId(), 1);
        if (increment>=3&&argpQualityInspectionReport.getInspectionResults().equals("合格")){
            argpQualityInspectionReport.setInspectionConclusion(1);

        }else{
            argpQualityInspectionReport.setInspectionConclusion(0);

        }
        Boolean save= argpCropsService.Recordadd(argpQualityInspectionReport);
        HashMap<String, Object> map = new HashMap<>();
        if (save){
            map.put("code",200);
            map.put("msg","添加成功");
        }else{
            map.put("code",500);
            map.put("msg","添加失败");
        }


        return map;

    }
    @RequestMapping("update")
    public HashMap<String, Object> update(@RequestBody ArgpCrops argpCrops){
        Boolean save= argpCropsService.update(argpCrops);
        HashMap<String, Object> map = new HashMap<>();
        if (save){
            map.put("code",200);
            map.put("msg","修改成功");
        }else{
            map.put("code",500);
            map.put("msg","修改失败");
        }

        return map;

    }
    @RequestMapping("listgrowthupdate")
    public HashMap<String, Object> listgrowthupdate(@RequestBody ArgpCropGrowthRecords argpCropGrowthRecords){
        Boolean save= argpCropsService.listgrowthupdate(argpCropGrowthRecords);
        HashMap<String, Object> map = new HashMap<>();
        if (save){
            map.put("code",200);
            map.put("msg","修改成功");
        }else{
            map.put("code",500);
            map.put("msg","修改失败");
        }

        return map;

    }
    @RequestMapping("del")
    public HashMap<String, Object> del(String cropId){
        Boolean save= argpCropsService.del(cropId);
        HashMap<String, Object> map = new HashMap<>();
        if (save){
            map.put("code",200);
            map.put("msg","修改成功");
        }else{
            map.put("code",500);
            map.put("msg","修改失败");
        }

        return map;

    }

    @RequestMapping("listgrowthadd")
    public HashMap<String, Object> listgrowthadd(@RequestBody ArgpCropGrowthRecords argpCropGrowthRecords){
        Boolean save= argpCropsService.listgrowthadd(argpCropGrowthRecords);
        HashMap<String, Object> map = new HashMap<>();
        if (save){
            map.put("code",200);
            map.put("msg","添加成功");
        }else{
            map.put("code",500);
            map.put("msg","添加失败");
        }

        return map;

    }
    @RequestMapping("listgrowth")
    public PageInfo<ArgpCropGrowthRecords> listgrowth(Integer pageNum,String cropId){
        PageHelper.startPage(pageNum,5);
        String DDid = cropId.toString();
        List<ArgpCropGrowthRecords>list= argpCropsService.listgrowth(DDid);
        PageInfo<ArgpCropGrowthRecords> info = new PageInfo<>(list);
        return info;

    }

//    @RequestMapping("saveDept")
//    public Result saveDept(@RequestBody @Valid SysDept sysDept, BindingResult result){
//        if (result.hasErrors()){
//            System.out.println(result.hasErrors());
//            return Result.fail(result.getAllErrors().get(0).getDefaultMessage());
//        }
//        sysDeptService.saveDept(sysDept);
//        return Result.success();
//    }
//
//    @RequestMapping("getParentById")
//    public Result getParentById(@RequestParam Integer id){
//        SysDept sysDept=sysDeptService.getParentById(id);
//        return Result.success(sysDept);
//    }
//
//    @RequestMapping("getById")
//    public Result getById(@RequestParam Integer id){
//
//        SysDept sysDept=sysDeptService.getById(id);
//
//        return Result.success(sysDept);
//    }
//
//    @RequestMapping("getParent")
//    public Result getParent(){
//
//        List<Tree<String>> parents=sysDeptService.getParent();
//
//        return Result.success(parents);
//    }
//
//    @RequestMapping("delDept")
//    public Result delDept(@RequestParam Integer id){
//
//        sysDeptService.delDept(id);
//
//        return Result.success();
//    }
//
//    @RequestMapping("updateDept")
//    public Result updateDept(@RequestBody SysDept sysDept){
//        sysDeptService.updateById(sysDept);
//        return Result.success();
//    }
//
//
//    @GetMapping("download")
//    public void  download(HttpServletResponse response) throws Exception {
//        String fileName = URLEncoder.encode("部门管理", "UTF-8").replaceAll("\\+","%20");
//        response.setCharacterEncoding("UTF-8");
//        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
//        // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
//        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
//        List<SysDept> deptList = sysDeptService.getDeptList();
//        EasyExcel.write(response.getOutputStream(), SysDept.class).sheet("部门信息").doWrite(deptList);
//
//    }

}
